<?php

class XkBrowser 
{
	var $name = null;
	var $version = null;
	var $useragent = null;
	var $platform;
	var $aol = false;
	var $browsertype;
	
	function XkBrowser()
	{
		$agent = $_SERVER['HTTP_USER_AGENT'];
		$this->useragent = $agent;
	}
	
	function getBrowserOS()
	{
		$win = eregi("win", $this->useragent);
		$linux = eregi("linux", $this->useragent);
		$mac = eregi("mac", $this->useragent);
		$os2 = eregi("OS/2", $this->useragent);
		$beos = eregi("BeOS", $this->useragent);
		
		if($win){
			$this->platform = "Windows";
		}
		elseif ($linux){
			$this->platform = "Linux"; 
		}
		elseif ($mac){
			$this->platform = "Macintosh"; 
		}
		elseif ($os2){
			$this->platform = "OS/2"; 
		}
		elseif ($beos){
			$this->platform = "BeOS"; 
		}
		return $this->platform;
	}
	
	function isOpera()
	{	
		if (eregi("opera",$this->useragent)){
			$val = stristr($this->useragent, "opera");
			if (eregi("/", $val)){
				$val = explode("/",$val);
				$this->browsertype = $val[0];
				$val = explode(" ",$val[1]);
				$this->version = $val[0];
			}
			else{
				$val = explode(" ",stristr($val,"opera"));
				$this->browsertype = $val[0];
				$this->version = $val[1];
			}
			return true;
		}
		else {
			return false;
		}
	}
	
	function isFirefox()
	{
		if(eregi("Firefox", $this->useragent)){
			$this->browsertype = "Firefox"; 
			$val = stristr($this->useragent, "Firefox");
			$val = explode("/",$val);
			$this->version = $val[1];
			return true;
		}
		else {
			return false;
		}
	}
	
	function isChrome()
	{
		if(eregi("Chrome", $this->useragent)){
			$this->browsertype = "Chrome"; 
			$val = stristr($this->useragent, "Chrome");
			$val = explode("/",$val);
			$val = explode(" ",$val[1]);
			$this->version = $val[0];
			return true;
		}
		else {
			return false;
		}
	}
	
	function isKonqueror()
	{
		if(eregi("Konqueror",$this->useragent)){
			$val = explode(" ",stristr($this->useragent,"Konqueror"));
			$val = explode("/",$val[0]);
			$this->browsertype = $val[0];
			$this->version = str_replace(")","",$val[1]);
			return true;
		}
		else {
			return false;
		}
		
	}
	
	function isIEv1()
	{
		if(eregi("microsoft internet explorer", $this->useragent)){
			$this->browsertype = "MSIE"; 
			$this->version = "1.0";
			$var = stristr($this->useragent, "/");
			if (ereg("308|425|426|474|0b1", $var)){
				$this->version = "1.5";
			}
			return true;
		}
		else {
			return false;
		}
	}
	
	function isMSIE()
	{
		if(eregi("msie", $this->useragent) && !eregi("opera",$this->useragent)){
			$this->browsertype = "MSIE"; 
			$val = explode(" ",stristr($this->useragent,"msie"));
			$this->browsertype = $val[0];
			$this->version = $val[1];
			
			return true;
		}
		else {
			return false;
		}
	}
	
	function isGaleon()
	{
		if(eregi("galeon",$this->useragent)){
			$val = explode(" ",stristr($this->useragent,"galeon"));
			$val = explode("/",$val[0]);
			$this->browsertype = $val[0];
			$this->version = $val[1];
			return true;
		}
		else {
			return false;
		}
	}
	
	function isWebTV()
	{
		if(eregi("webtv",$this->useragent)){
			$val = explode("/",stristr($this->useragent,"webtv"));
			$this->browsertype = $val[0];
			$this->version = $val[1];
			return true;
		}
		else {
			return false;
		}
	}
	
	function isNetPositive()
	{
		if(eregi("NetPositive", $this->useragent)){
			$val = explode("/",stristr($this->useragent,"NetPositive"));
			$this->platform = "BeOS"; 
			$this->browsertype = $val[0];
			$this->version = $val[1];
			return true;
		}
		else {
			return false;
		}
	}
	
	function isMSPIE()
	{
		if(eregi("mspie",$this->useragent) || eregi("pocket", $this->useragent)){
			$val = explode(" ",stristr($this->useragent,"mspie"));
			$this->browsertype = "MSPIE"; 
			$this->platform = "WindowsCE"; 
			if (eregi("mspie", $this->useragent)){
				$this->version = $val[1];
			}
			else {
				$val = explode("/",$this->useragent);
				$this->version = $val[1];
			}
			return true;
		}
		else {
			return false;
		}
	}
	
	function isIcab()
	{
		if(eregi("icab",$this->useragent)){
			$val = explode(" ",stristr($this->useragent,"icab"));
			$this->browsertype = $val[0];
			$this->version = $val[1];
			return true;
		}
		else {
			return false;
		}
	}
	
	function isOmniWeb()
	{
		if(eregi("omniweb",$this->useragent)){
			$val = explode("/",stristr($this->useragent,"omniweb"));
			$this->browsertype = $val[0];
			$this->version = $val[1];
			return true;
		}
		else {
			return false;
		}
	}
	
	function isPhoenix()
	{
		if(eregi("Phoenix", $this->useragent)){
			$this->browsertype = "Phoenix"; 
			$val = explode("/", stristr($this->useragent,"Phoenix/"));
			$this->version = $val[1];
			return true;
		}
		else {
			return false;
		}
	}
	
	function isFirebird()
	{
		if(eregi("firebird", $this->useragent)){
			$this->browsertype = "Firebird"; 
			$val = stristr($this->useragent, "Firebird");
			$val = explode("/",$val);
			$this->version = $val[1];
			return true;
		}
		else {
			return false;
		}
	}
	
	function isMozAlphaBeta()
	{
		if(eregi("mozilla",$this->useragent) && 
		   eregi("rv:[0-9].[0-9][a-b]",$this->useragent) && 
		   !eregi("netscape",$this->useragent))
		{
			$this->browsertype = "Mozilla"; 
			$val = explode(" ",stristr($this->useragent,"rv:"));
			eregi("rv:[0-9].[0-9][a-b]",$this->useragent,$val);
			$this->version = str_replace("rv:","",$val[0]);
			return true;
		}
		else {
			return false;
		}
	}

	function isMozStable()
	{
		if(eregi("mozilla",$this->useragent) &&
		   eregi("rv:[0-9]\.[0-9]",$this->useragent) && 
		   !eregi("netscape",$this->useragent))
		{
			$this->browsertype = "Mozilla"; 
			$val = explode(" ",stristr($this->useragent,"rv:"));
			eregi("rv:[0-9]\.[0-9]\.[0-9]",$this->useragent,$val);
			$this->version = str_replace("rv:","",$val[0]);
			return true;
		}
		else {
			return false;
		}
	}
	
	function isLynx()
	{
		if(eregi("libwww", $this->useragent)){
			if (eregi("amaya", $this->useragent)){
				$val = explode("/",stristr($this->useragent,"amaya"));
				$this->browsertype = "Amaya"; 
				$val = explode(" ", $val[1]);
				$this->version = $val[0];
			} 
			else {
				$val = explode("/",$this->useragent);
				$this->browsertype = "Lynx"; 
				$this->version = $val[1];
			}
			return true;
		}
		else {
			return false;
		}
	}
	
	function isSafari()
	{
		if(eregi("safari", $this->useragent)){
			$this->browsertype = "Safari"; 
			$this->version = "";
			return true;
		}
		else {
			return false;
		}
	}
	
	function isNetscape()
	{
		if(eregi("netscape",$this->useragent)){
			$val = explode(" ",stristr($this->useragent,"netscape"));
			$val = explode("/",$val[0]);
			$this->browsertype = $val[0];
			$this->version = $val[1];
			return true;
		}
		else if(eregi("mozilla",$this->useragent) && 
				!eregi("rv:[0-9]\.[0-9]\.[0-9]",$this->useragent))
		{
			$val = explode(" ",stristr($this->useragent,"mozilla"));
			$val = explode("/",$val[0]);
			$this->browsertype = "Netscape"; 
			$this->version = $val[1];
			return true;
		}
		else {
			return false;
		}
	}
	
	function isAOL()
	{
		if (eregi("AOL", $this->useragent)){
			$var = stristr($this->useragent, "AOL");
			$var = explode(" ", $var);
			$this->aol = ereg_replace("[^0-9,.,a-z,A-Z]", "", $var[1]);
			return true;
		}
		else { 
			return false;
		}
	}
	
	function whatBrowser()
	{
		$this->getBrowserOS();
		$this->isOpera();
		$this->isFirefox();
		$this->isKonqueror();
		$this->isIEv1();
		$this->isMSIE();
		$this->isGaleon();
		$this->isNetPositive();
		$this->isMSPIE();
		$this->isIcab();
		$this->isOmniWeb();
		$this->isPhoenix();
		$this->isFirebird();
		$this->isLynx();
		$this->isSafari();
		$this->isChrome();
		$this->isAOL();
		return array('browsertype' => $this->browsertype, 
					 'version' => $this->version, 
					 'platform' => $this->platform, 
					 'AOL' => $this->aol); 
	}
}

?>